package cn.sinjet.prototol.codec;

import cn.sinjet.communication.bluetooth.BluetoothModel;

public class ToureDevCodec {
	private void packAndSendMsg(byte[] message,int len){
	    byte[] szMsg = new byte[len + 5];
		szMsg[0] = (byte) 0xFF;
		szMsg[1] = (byte) 0x55;
		szMsg[2] = (byte) (len + 1);
		szMsg[3] = (byte) 0x03;
		System.arraycopy(message, 0, szMsg, 4, len);

		byte sum = 0;
		int s = 0;
		int i = 0;
		for (i = 2; i < len + 4; i++) {
			s += (szMsg[i] & 0xFF);
		}
		sum = (byte) (s & 0xFF);
		szMsg[len + 4] = sum;
		BluetoothModel.getInstance().sendMessage(szMsg);
    }
	public void sendInit(int flag){
		byte[] buf = new byte[]{0x0,(byte)flag};
        packAndSendMsg(buf, 2);		
	}
	
	public void sendAppPage(int pageId){
		byte[] buf = new byte[]{0x0D,(byte)pageId};
		packAndSendMsg(buf, 2);
	}
	public void sendConnectECU(int flag) {
		// TODO Auto-generated method stub
		byte[] buf = new byte[]{0x02,(byte)flag};
		packAndSendMsg(buf, 2);
	}
	public void sendClearErr() {
		// TODO Auto-generated method stub
		byte[] buf = new byte[]{0x06,0x00};
		packAndSendMsg(buf, 2);
	}
	public void sendStartFastDetect() {
		// TODO Auto-generated method stub
		byte[] buf = new byte[]{0x0E,0x00};
		packAndSendMsg(buf, 2);
	}
	public void sendHeartbeat(){
		byte[] buf = new byte[]{(byte)0xAA,0x00};
		packAndSendMsg(buf, 2);
	}
}
